import openpyxl


def read_excel_dict(path:str):
    """读取Excel数据，存储成元素是字典的列表 --- [{}, {}, {}]"""
    # 实例化一个workbook
    workbook = openpyxl.load_workbook(path, read_only=True)
    # 实例化一个sheet
    sheet = workbook['student']
    # 定义一个变量存储最终的数据
    students = []
    keys = ['sno', 'name', 'gender', 'birthday', 'mobile', 'email', 'address', 'image']
    # 遍历
    for row in sheet.rows:
        # 定义一个临时字典
        temp_dict = {}
        # 组合值和key
        for index, cell in enumerate(row):
            temp_dict[keys[index]] = cell.value
        # 附加到list中
        students.append(temp_dict)
        # 返回
    return students


if __name__ == '__main__':
    path = "C:/Users/Liang/Desktop/勤/卷/py/Django/前后端分离/BE/media/test1.xlsx"
    students = read_excel_dict(path)
    # 输出
    print(students)